/**
 * Created by wry on 2015/1/13.
 */

var CSOpsService = require('../../services/BI/BI-CSOpsService');
var messageUtil = require('../../common/utils/messageUtil');
var toolUtil = require('../../common/utils/toolUtil');
var moment = require('moment');

Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length == 1 ? o[k] :
                ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

exports.getTopUsersReport = function(req, res){
    var community = req.params.community;
    var numPerPage = req.params.numPerPage;
    var filter = req.params.filt;

    CSOpsService.getTopUsersService(community, numPerPage, filter, function (err, results) {
        if (err) {
            logger.error('Exception happened when getTopUsersReport...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getTopUsersReport...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
}

exports.getTopQuestionsReport = function(req, res){
    var community = req.params.community;
    var numPerPage = req.params.numPerPage;
    var filter = req.params.filt;

    CSOpsService.getTopQuestionsService(community, numPerPage, filter, function (err, results) {
        if (err) {
            logger.error('Exception happened when getTopQuestionsReport...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getTopQuestionsReport...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
}

exports.getTopActivitiesReport = function(req, res){
    var community = req.params.community;
    var numPerPage = req.params.numPerPage;
    var filter = req.params.filt;

    CSOpsService.getTopActivitiesService(community, numPerPage, filter, function (err, results) {
        if (err) {
            logger.error('Exception happened when getTopActivitiesReport...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getTopActivitiesReport...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, results));
        }
    });
}
